package com.zsxb.crm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zsxb.crm.dto.DeptDTO;
import com.zsxb.crm.mapper.DeptMapper;
import com.zsxb.crm.mapper.RoleMapper;
import com.zsxb.crm.pojo.Dept;
import com.zsxb.crm.result.PageResult;
import com.zsxb.crm.service.DeptService;


import com.zsxb.crm.vo.TreeselectVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private RoleMapper roleMapper;
    //查询部门
    @Override
    public List<Dept> getDeptList(DeptDTO deptDTO) {

        List<Dept> dept = deptMapper.getDeptList(deptDTO);
        return dept;
    }
    //新增部门
    @Override
    public void insert(DeptDTO deptDTO) {
        deptDTO.setCreateBy("admin");//测试
         deptMapper.insert(deptDTO);
    }
    //根据部门id查询部门
    @Override
    public Dept excludeById(Long deptId) {
          return deptMapper.getById(deptId);
    }
    //修改部门
    @Override
    public void update(DeptDTO deptDTO) {
        deptDTO.setUpdateBy("admin");
        deptDTO.setUpdateTime(LocalDateTime.now());
        deptMapper.update(deptDTO);

    }
    //删除部门
    @Override
    public void delete(Long id) {
       deptMapper.delete(id);
    }

    @Override
    public List<TreeselectVO> list() {
        List<TreeselectVO> deptList = deptMapper.treeselect();
        List<TreeselectVO> list = treeselectVOList(deptList);
        return list;
    }
    //分页查询部门
    @Override
    public PageResult getDepts(DeptDTO deptDTO) {
        PageHelper.startPage(deptDTO.getPageNum(),deptDTO.getPageSize());
        Page<Dept> depts =  deptMapper.getDepts(deptDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(depts.getTotal());
        pageResult.setRows(depts.getResult());
         return pageResult;

    }

    @Override
    public List<TreeselectVO> roleDeptTreeselect(Integer roleId) {
        List<TreeselectVO> list  = roleMapper.getByRoleId(roleId);
        log.info("根据角色id查询部门结果{}",list);
        List<TreeselectVO> treeselectVOS = treeselectVOList(list);
        return treeselectVOS;
    }



    public static List<TreeselectVO> treeselectVOList(List<TreeselectVO> deptList){
        List<TreeselectVO> resultJsonList = new ArrayList<>();
        deptList.forEach(d -> {
            if (d != null && d.getParentId() == 0) {
                resultJsonList.add(findChildren(d, deptList));
            }
        });
        return resultJsonList;
    }

    private static TreeselectVO findChildren(TreeselectVO parent, List<TreeselectVO> deptList) {
        List<TreeselectVO> children = new ArrayList<>();
        deptList.forEach(d -> {
            if (d != null && parent.getId().equals(d.getParentId())) {
                children.add(findChildren(d, deptList));
            }
        });
        parent.setChildren(children);
        return parent;
    }
}
